﻿using FastExecutor.Base.Design;
using FastExecutor.Base.Model;
using FastExecutor.Base.Util;
using FastExecutor.Message.Application;
using FastExecutor.ORG.Entity;
using FastExecutor.ORG.Enum;
using FastUtil.Base;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Hosting;
using System.Web.Mvc;

namespace FastExecutor.ORG.Annotation
{
    public class ErrorFilter : HandleErrorAttribute
    {
        public override void OnException(ExceptionContext filterContext)
        {
            base.OnException(filterContext);
            //标记异常已处理
            filterContext.ExceptionHandled = true;
            StringBuilder ExceptionLog = new StringBuilder();
            ExceptionLog.AppendFormat(Environment.NewLine);

            Frame_ErrorLog LogEnity = new Frame_ErrorLog();
            LogEnity.RowGuid = CommonUtil.CreateCommonGuid();
            BaseController controller = (BaseController)filterContext.Controller;
            LogEnity.UGuid = controller.UserGuid;
            LogEnity.UName = controller.DisplayName;
            LogEnity.DGuid = controller.OUGuid;
            LogEnity.DName = controller.OUName;
            LogEnity.ErrorDate = DateTime.Now;
            LogEnity.FromIP = CommonUtil.GetClientIP();
            LogEnity.ErrorUrl = filterContext.HttpContext.Request.Url.ToString();
            if (filterContext.HttpContext.Request.IsAjaxRequest())
            {
                //ajax请求
                ExceptionLog.AppendFormat("请求类型:ajax请求！" + Environment.NewLine);
                ReturnValue result = new ReturnValue();
                result.Add("code",-999);
                result.Add("msg", "系统发生错误，请联系管理员");
                filterContext.Result = result;
                LogEnity.ErrorType = (int)FrameEnum.ErrorType.CodeError;
            }
            else
            {
                //页面等资源请求
                ExceptionLog.AppendFormat("请求类型:页面或静态资源请求！" + Environment.NewLine);
                LogEnity.ErrorType = (int)FrameEnum.ErrorType.PageError;
            }

            ExceptionLog.AppendFormat("异常请求url:{0}", filterContext.RequestContext.HttpContext.Request.Url + Environment.NewLine);
            ExceptionLog.AppendFormat("异常信息Message:{0}", filterContext.Exception.Message + Environment.NewLine);
            ExceptionLog.AppendFormat("异常信息Trace:{0}", filterContext.Exception.StackTrace + Environment.NewLine);
            ExceptionLog.AppendFormat("========================================================================================================================================================================" + Environment.NewLine);
            LogEnity.ErrorContent = ExceptionLog.ToString();
            Dictionary<string,string> ExtraData = new Dictionary<string,string>();
            ExtraData.Add("LogFolder", "CodeErrorLog");
            MessageCenter.GetInstance().SendMessage(FrameEnum.LogType.Error.ToString(), LogEnity, ExtraData);
        }
    }
}
